package com.example.task;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class TimeTask {

    // 每秒输出一次时间，持续 durationSeconds 秒
    public void printTimeEverySecond(int durationSeconds) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < durationSeconds; i++) {
            System.out.println("当前时间: " + sdf.format(new Date()));
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
        }
    }

    // 倒计时 seconds 秒
    public void countdown(int seconds) {
        for (int i = seconds; i > 0; i--) {
            System.out.println("倒计时: " + i + " 秒");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
        }
        System.out.println("倒计时结束！");
    }
}
    